<!DOCTYPE html>
<html lang="en">
<head>
    <title>three.js webgl - panorama</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <style>
        html, body {
            background-color: #000;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>

</head>
<body>
<div id="container"></div>
</body>

<script src="https://johnson2heng.github.io/three.js-demo/lib/three.js"></script>

<script>


    var camera, scene, renderer;

    var isUserInteracting = false,
        onMouseDownMouseX = 0, onMouseDownMouseY = 0,
        lon = 0, onMouseDownLon = 0,
        lat = 0, onMouseDownLat = 0,
        phi = 0, theta = 0;

    init();
    animate();

    function init() {

        var container;

        container = document.getElementById( 'container' );

        camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 1, 1100 );
        camera.target = new THREE.Vector3( 0, 0, 0 );

        scene = new THREE.Scene();

        var materials = getTextures(
            [
                "../images/skybox/px.jpg",
                "../images/skybox/nx.jpg",
                "../images/skybox/py.jpg",
                "../images/skybox/ny.jpg",
                "../images/skybox/pz.jpg",
                "../images/skybox/nz.jpg"
            ]
        );

        var skyBox = new THREE.Mesh(new THREE.CubeGeometry(200, 200, 200), materials);
        skyBox.applyMatrix(new THREE.Matrix4().makeScale(1, 1, -1));
        scene.add(skyBox);

        renderer = new THREE.WebGLRenderer();
        renderer.setPixelRatio( window.devicePixelRatio );
        renderer.setSize( window.innerWidth, window.innerHeight );
        container.appendChild( renderer.domElement );

        document.addEventListener( 'mousedown', onDocumentMouseDown, false );
        document.addEventListener( 'mousemove', onDocumentMouseMove, false );
        document.addEventListener( 'mouseup', onDocumentMouseUp, false );
        document.addEventListener( 'wheel', onDocumentMouseWheel, false );

        window.addEventListener( 'resize', onWindowResize, false );

    }

    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();

        renderer.setSize( window.innerWidth, window.innerHeight );

    }

    function onDocumentMouseDown( event ) {

        event.preventDefault();

        isUserInteracting = true;

        onMouseDownMouseX = event.clientX;
        onMouseDownMouseY = event.clientY;

        onMouseDownLon = lon;
        onMouseDownLat = lat;

    }

    function onDocumentMouseMove( event ) {

        if ( isUserInteracting === true ) {


            lon = ( onMouseDownMouseX - event.clientX ) * 0.1 *camera.fov/100 + onMouseDownLon;
            lat = ( event.clientY - onMouseDownMouseY ) * 0.1 *camera.fov/100 + onMouseDownLat;

        }

    }

    function onDocumentMouseUp( event ) {

        isUserInteracting = false;

    }

    function onDocumentMouseWheel( event ) {

        //event.deltaY 触发wheel事件时，使用deltaY属性返回鼠标滚轮的垂直滚动量（Y轴）。 一般鼠标上的滚轮为前后滚轮，往前滚动时deltaY的值为负值，往后滚动为正值。
        var fov = camera.fov + event.deltaY * 0.03;

        //THREE.Math.clamp 如果x小于a，返回a。 如果x大于b，返回b，否则返回x
        camera.fov = THREE.Math.clamp( fov, 10, 120 );

        camera.updateProjectionMatrix();

    }

    function animate() {

        requestAnimationFrame( animate );
        update();

    }

    function update() {

        if ( isUserInteracting === false ) {

            lon += 0.1;

        }

        lat = THREE.Math.clamp( lat, - 89.9, 89.9 );
        phi = THREE.Math.degToRad( 90 - lat ); //返回当前角度相同的弧度值
        theta = THREE.Math.degToRad( lon ); //返回当前角度相同的弧度值

        camera.target.x = 100 * Math.sin( phi ) * Math.cos( theta );
        camera.target.y = 100 * Math.cos( phi );
        camera.target.z = 100 * Math.sin( phi ) * Math.sin( theta );

        camera.lookAt( camera.target );

        //console.log(lon%360,lat,camera.fov,camera.target);

        renderer.render( scene, camera );

    }

    function getTextures(arr) {
        //判断一下是否有数组传入，如果有则按照数组传入的图形进行绘制
        let uvArr = [];
        if (arr) {
            uvArr = arr;
        }
        else {
            //如果没有传入，则使用默认生成的一组
            let uv = ["r", "l", "u", "d", "b", "f"];
            let dir = "images/100000_2/";
            let jpg = ".jpg";
            for (let i = 0; i < uv.length; i++) {
                uvArr[i] = dir + uv[i] + jpg;
            }
        }

        let textureLoader = new THREE.TextureLoader();

        let materials = [];
        for (let i = 0, len = uvArr.length; i < len; i++) {
            materials.push(new THREE.MeshBasicMaterial({map: textureLoader.load(uvArr[i])}));
        }

        return materials;
    }

</script>
</html>